package tools;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import api.ResultXml;

/**
 * 
 * Description: 根据参数t的DTO类来输出xml文档。
 *  
 * since  2009/2/28
 * author ashen
 * 
 */
public class OutputXml { 
	
	/**
	 * the method output of class OutputXml
	 * 
	 * 根据参数t的DTO类来输出xml文档
	 * 
	 * @param <T> 专门用来输出的DTO类型
	 * @param t 专门用来输出的DTO类的对象
	 * @param response 服务器servlet对用户请求的响应
	 */
	public static <T> void output(T t,HttpServletResponse response){
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			Document document = ResultXml.getXml(t);
			XMLOutputter outputter = new XMLOutputter();
			Format format = Format.getPrettyFormat();
			format.setEncoding("UTF-8");//default=UTF-8
			outputter.setFormat(format);
			outputter.output(document, out);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		out.close();
	}
}
